package cn.jsprun.dao.otherset;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.jsprun.domain.Advertisements;
import cn.jsprun.utils.HibernateUtil;

public class AdvSetDaoImpl implements AdvSetDao {
		
	public boolean addAdv(Advertisements adv) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			session.save(adv);
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean delAnyAdvs(String[] deleteIds) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			int len = deleteIds.length;
			Advertisements adv = null;
			for(int i=0;i<len;i++){
				adv = (Advertisements)session.get(Advertisements.class, Integer.parseInt(deleteIds[i]));
				session.delete(adv);
			}
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}
	
	public Advertisements queryAdvById(Integer id) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			Advertisements adv = (Advertisements) session.get(Advertisements.class, id);
			ts.commit();
			return adv;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Advertisements> queryAllAdv() {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			List<Advertisements> advList = session.createQuery("from Advertisements order by displayorder,starttime desc,title desc").list();
			ts.commit();
			return advList;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Advertisements> queryAdvByCondition(String sqlStatement) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			List<Advertisements> adList = session.createSQLQuery(sqlStatement).addEntity(Advertisements.class).list();
			ts.commit();
			return adList;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		
		return null;
	}
	
	public boolean updateAdv(Advertisements adv) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			session.update(adv);
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateAdvs(List<Advertisements> advList) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			Iterator<Advertisements> advIt = advList.iterator();
			Advertisements adv = null;
			while(advIt.hasNext()){
				adv = advIt.next();
				session.update(adv);
			}
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}

	

}
